package per.turtle.system.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import per.turtle.common.core.web.controller.BaseController;
import per.turtle.common.core.web.entity.CommonResult;
import per.turtle.common.core.web.page.TableDataInfo;
import per.turtle.common.excel.annotation.ResponseExcel;
import per.turtle.common.log.annotation.Log;
import per.turtle.common.log.enums.BusinessType;
import per.turtle.common.security.annotation.PreAuthorize;
import per.turtle.system.entity.SysOperLog;
import per.turtle.system.service.ISysOperLogService;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author turtle
 */
@AllArgsConstructor
@RequestMapping("operLog")
@RestController
@Tag(name = "操作日志管理", description = "operLog_操作日志管理模块")
public class SysOperLogController extends BaseController {

    private final ISysOperLogService operLogService;

    @Operation(summary = "添加操作日志", description = "添加操作日志")
    @PostMapping
    public CommonResult<Void> add(@RequestBody SysOperLog operLog) {
        return result(operLogService.insertOperLog(operLog));
    }

    @Operation(summary = "清空操作日志", description = "清空操作日志")
    @DeleteMapping("clean")
    @Log(title = "操作日志", businessType = BusinessType.CLEAN)
    @PreAuthorize(hasPermission = "system:operLog:remove")
    public CommonResult<Void> clean() {
        operLogService.cleanOperLog();
        return CommonResult.success();
    }

    @Operation(summary = "导出操作日志", description = "导出操作日志")
    @Log(title = "操作日志", businessType = BusinessType.EXPORT)
    @PostMapping("export")
    @PreAuthorize(hasPermission = "system:operLog:export")
    @ResponseExcel("操作日志")
    public List<SysOperLog> export(HttpServletResponse response, @RequestBody SysOperLog operLog) {
        return operLogService.selectOperLogList(operLog);
    }

    @Operation(summary = "获取操作日志列表或分页", description = "获取操作日志列表或分页（区别于是否传入分页参数）")
    @PostMapping("list")
    @PreAuthorize(hasPermission = "system:operLog:list")
    public TableDataInfo<SysOperLog> list(@RequestBody SysOperLog operLog) {
        List<SysOperLog> list = startPage(s -> operLogService.selectOperLogList(operLog));
        return getDataTable(list);
    }

    @Operation(summary = "删除操作日志", description = "删除操作日志")
    @DeleteMapping("{operIds}")
    @Log(title = "操作日志", businessType = BusinessType.DELETE)
    @PreAuthorize(hasPermission = "system:operLog:remove")
    public CommonResult<Void> remove(@PathVariable List<Long> operIds) {
        return result(operLogService.deleteOperLogByIds(operIds));
    }

}
